package com.demoworld.dao;

import com.demoworld.entity.HelloKitty;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.net.UnknownHostException;

/**
 * Created by francis on 2017/6/25.
 */
public class MongoDao {
    public MongoDao() {
        Mongo mongo= null;
        try {
            mongo = new Mongo("192.168.222.130", 27017);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        mongoTemplate = new MongoTemplate(mongo, "db_dev");
    }
    private static String HELLOKITTY = "hellokitty";
    private MongoTemplate mongoTemplate;
    public void createHelloKitty(HelloKitty hello) {
        mongoTemplate.insert(hello, HELLOKITTY);
    }
    public HelloKitty getHelloKittyByName(String name) {
        return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), HelloKitty.class, HELLOKITTY);
    }
    public int deleteByName(String name){
        WriteResult er=  mongoTemplate.remove(new Query(Criteria.where("name").is(name)), HelloKitty.class, HELLOKITTY);
        return er.getN();
    }
}